summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/uic/barcode/utils/AlgorithmNameResolver.java
blob: 28f90e697f737dd26982f2d3e0e8efaa273d67f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.uic.barcode.utils;

import java.security.Provider;
import java.security.Provider.Service;
import java.security.Security;
import java.util.HashMap;

/**
 * The Class AlgorithmNameResolver.
 */
public class AlgorithmNameResolver {
	
	
	/** The Constant TYPE_KEY_GENERATOR_ALG. */
	public static final String TYPE_KEY_GENERATOR_ALG = "KeyPairGenerator";
	
	/** The Constant TYPE_SIGNATURE_ALG. */
	public static final String TYPE_SIGNATURE_ALG = "Signature";
	
	/** The map. */
	private static HashMap<String, HashMap<String, String>> map = new HashMap<String, HashMap<String, String>>();
	
	
	/**
	 * Adds an entry for a mapping of algorithm type and oid to an algorithm name
	 *
	 * @param type the algorithm type
	 * @param oid the algorithm OID
	 * @param name the algorithm name
	 */
	public static void addMap (String type, String oid, String name) {
		
		if (map.get(type) == null) {
			HashMap<String,String> list = new HashMap<String , String>(); 
			list.put(oid,  name);
			map.put(type, list);
		} else {
			map.get(type).put(oid, name);
		}
	}
	
    /**
     * Gets the signature algorithm name.
     *
     * @param oid the oid
     * @return the signature algorithm name
     * @throws Exception the exception
     */
    public static String getSignatureAlgorithmName (String oid) throws Exception {
    	
    	return getSignatureAlgorithmName (oid, null);
    	
    }
    
    /**
     * Gets the signature algorithm name.
     *
     * @param oid the oid
     * @param provider the provider
     * @return the signature algorithm name
     * @throws Exception the exception
     */
    public static String getSignatureAlgorithmName (String oid, Provider provider) throws Exception {
    	
    	return getAlgorithmName(AlgorithmNameResolver.TYPE_SIGNATURE_ALG,oid,provider);
     	
    }
    
    /**
     * Gets the name.
     *
     * @param type the type
     * @param oid the oid
     * @return the name
     * @throws Exception the exception
     */
    public static String getName (String type, String oid) throws Exception {
    	
    	return getAlgorithmName(type,oid,null);
    	
    }
    	
    
    /**
     * Gets the name.
     *
     * @param type the type
     * @param oid the oid
     * @param provider the provider
     * @return the name
     * @throws Exception the exception
     */
    public static String getName(String type, String oid, Provider provider) throws Exception {
	   
	   return getAlgorithmName(type,oid,provider);
	
	}    
    
    
    
    /**
     * Gets the algorithm name.
     *
     * @param type the type
     * @param oid the oid
     * @param provider the provider
     * @return the algorithm name
     * @throws Exception the exception
     */
    public static String getAlgorithmName (String type, String oid, Provider provider) throws Exception {
    	
    	String name = null;
    	
    	if (provider != null) {
    		 Service service = provider.getService(type,oid);
    		 if (service != null) {
 		    	 name = service.getAlgorithm();
 		     }
    		 
    		 if (name != null && name.length() > 0) {
    			 return name;
    		 }
    	}
    	
    	
 		Provider[] provs = Security.getProviders();
 		for (Provider prov : provs) {
 		       Service service = prov.getService(type,oid);
 		       if (service != null) {
 		    	 name = service.getAlgorithm();
 		       }		    	    		    		 
 		       if (name != null && name.length() > 0) {
 		    	 return name;
 		       }
 		}
 		 		
 		if (map.get(type) != null) {
 			if (map.get(type).get(oid) != null) {
 				return map.get(type).get(oid);
 			}
 		}
 		
 		
		//fallback if the provider did not implement OIDs
		if (oid.startsWith("1.2.840.10045.4")) {
			return "ECDSA";
		} else if (oid.startsWith("1.2.840.10045.3")) {
			return "EC";
		} else if (oid.startsWith("1.2.840.10045")) {
			return "ECDSA";
		} else if (oid.startsWith("1.2.840.10040")) {
			return "DSA";
		}
 		
 		return null;
     	
     }
    
    
 
  


}